/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet.Ultility;
import Model.DataUltility.*;
import com.sun.accessibility.internal.resources.accessibility;
import com.sun.crypto.provider.RSACipher;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author chickencake1
 */
public class Checkout {
    Data_Access access;
    public Checkout() {
        access = new Data_Access();
    }
    
    public void product_Update(String column,int newData,String conditioncol,String condition_data)
    {
        int stock=getStock(condition_data);
        int value;
        if(stock>=newData)
            value=stock-newData;
        else
            value=0;
        
        int sold=getSold(condition_data)+newData;
        
        String []Column={column};
        int []new_data={value};
        int []new_data1={sold};
        String []Column1={"sold_number"};
        String []Condition_col={conditioncol};
        String []Condition_data={condition_data};
        
        access.updateInformation("resource", Column, new_data, Condition_col, Condition_data,null);
        access.updateInformation("resource", Column1, new_data1, Condition_col, Condition_data,null);
    }

    public int getStock(String ID)
    {
        try {
            String []colum={"re_id"};
            String []data={ID};
            ResultSet rs=access.getInformation("resource", colum, data);
            if(rs.next())
            {
                return Integer.parseInt(rs.getString("stock").toString());
            }
        } catch (SQLException ex) {
            return 0;
        }
        return 0;
    
    }
    
        public int getSold(String ID)
    {
        try {
            String []colum={"re_id"};
            String []data={ID};
            ResultSet rs=access.getInformation("resource", colum, data);
            if(rs.next())
            {
                return Integer.parseInt(rs.getString("sold_number").toString());
            }
        } catch (SQLException ex) {
            return 0;
        }
        return 0;
    
    }
    public boolean isConflict(String table,String columName,String id)
    {
        try {
            String [] column={columName};
            String [] data={id};
           ResultSet rs= access.getInformation(table, column, data);
           if(rs.next())
               return true;
        } catch (SQLException ex) {
            Logger.getLogger(Checkout.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
       return false;
    }
}
